(function(_window, _document) {
    "use strict";
    var then;
    var thenMonster;
    var ctx;
    var thread;
    var keysDown = {};
    
    _window.game = function(parent) {
        var canvas = _document.createElement("canvas");
        ctx = canvas.getContext("2d");
        canvas.width = 512;
        canvas.height = 480;
        parent.appendChild(canvas);
        thenMonster = then = Date.now();
        var monster = {
            x : getRandomInt(1, 500) + 5,
            y : getRandomInt(1, 470) + 5
        }
        monsters.push(monster);
        thread = setInterval(main, 1);
    };
    
    var main = function() {
        var now = Date.now();
        var delta = now - then;
        update(delta / 1000);
        render();
        then = now;
    };
    
    var update = function(modifier) {
        if (38 in keysDown) { // up
            hero.y -= hero.speed * modifier;
        }
        if (40 in keysDown) { // down
            hero.y += hero.speed * modifier;
        }
        if (37 in keysDown) { // left
            hero.x -= hero.speed * modifier;
        }
        if (39 in keysDown) { // right
            hero.x += hero.speed * modifier;
        }
        if (32 in keysDown) { //space
            for (var i = 0; i < monsters.length; i++) {
                if (Math.abs(hero.x - monsters[i].x) < 10 && Math.abs(hero.y - monsters[i].y) < 10) {
                    gameStatistic.monstersCaught = ++gameStatistic.monstersCaught;
                    monsters.splice(i, 1);
                }
            }
        } else {
            if (hero.damaged == -1) {
                for (var i = 0; i < monsters.length; i++) {
                    if (Math.abs(hero.x - monsters[i].x) < 7 && Math.abs(hero.y - monsters[i].y) < 7) {
                        hero.vitality = hero.vitality - 1;
                        console.log(hero.vitality);
                        if (hero.vitality <= 0) {
                            clearInterval(thread);
                        } else {
                            hero.damaged = Date.now();
                        }
                    }
                }
            }
        }
        var now = Date.now();
        if (now - thenMonster > 10000) {
            var monster = {
                x : getRandomInt(1, 500) + 5,
                y : getRandomInt(1, 470) + 5
            }
            monsters.push(monster);
            thenMonster = now;
        }
        if (now - hero.damaged > 1000) {
            hero.damaged = -1;
        }
    };
    
    var render = function() {
        ctx.fillStyle = "rgb(0, 250, 0)";
        ctx.fillRect(0, 0, 512, 480);
        ctx.fillStyle = "rgb(0, 0, 250)";
        ctx.fillRect(hero.x - 3, hero.y - 3, 6, 6);
        ctx.fillStyle = "rgb(250, 0, 0)";
        for (var i = 0; i < monsters.length; i++) {
            ctx.fillRect(monsters[i].x - 3, monsters[i].y - 3, 6, 6);
        }
    };
    
    var getRandomInt = function(min, max) {
        return Math.floor(Math.random() * (max - min + 1)) + min;
    }

    
    var hero = {
        damaged: -1,
        vitality: 3,
        speed: 64,
        side: 0,
        x : 0,
        y : 0
    };
    
    var monsters = new Array();
    
    var gameStatistic = {
        monstersCaught : 0
    };
    
    _window.addEventListener ("keydown", function (e) {
        keysDown[e.keyCode] = true;
    }, false);
    _window.addEventListener ("keyup", function (e) {
        delete keysDown[e.keyCode];
    }, false);
})(window, document);
